<template>
    <div class="role-list">
        <Row>
            <Col :xs="24" :sm="12">
            <Form :model="formItem" ref="formItem" :inline="true" action="javascript:void(0)">
                <FormItem>
                    <Input v-model="formItem.key" placeholder="输入关键词查找..."></Input>
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="renderTable('formItem')">查找</Button>
                </FormItem>
            </Form>
            </Col>
        </Row>
        <!-- 批量操作 -->
        <div class="action-btn">
            <Button size="small" @click="deleteData" style="margin-right: 8px" type="error">批量删除</Button>
            <Button size="small" @click="showModel" type="success">添加角色</Button>
        </div>
        <Table border :columns="list" ref="roleList" :data="tablelist" @on-selection-change="changeData"
            :loading="dataloading">
            <template #status="{ row }">
                <i-switch :true-value="1" :false-value="0" v-model="row.status" size="large"
                    @on-change="changeStatus(row.id, $event)"></i-switch>
            </template>
            <template #action="{ row,index }">
                <Button type="success" size="small" style="margin-right: 5px;float:left"
                    @click="showAdmin(row.id)">用户</Button>
                <Button type="warning" size="small" style="margin-right: 5px;float:left"
                    @click="showPrivModal(row.id)">权限</Button>
                <Button type="info" size="small" style="margin-right: 5px;float:left"
                    @click="showEditModal(row.id)">修改</Button>
                <Button type="error" size="small" @click="remove(index, row.id)">删除</Button>
            </template>
        </Table>
        <!-- 分页 -->
        <div class="table-page">
            <Page size="small" ref="listPage" :total="pages.total" :current="pages.current" :page-size="pages.size"
                show-elevator show-total @on-change="changePage"></Page>
        </div>
        <!-- 添加的弹出 -->
        <!-- 需要全屏时添加这句 :mask="false" class-name="idw100" -->
        <Drawer :closable="false" :mask-closable="false" :scrollable="true" title="添加角色" width="640"
            v-model="showModalStatus">
            <Spin size="large" fix v-if="loading"></Spin>
            <Form :label-width="80" :model="role" ref="roleValidate" :rules="roleValidate" action="javascript:void(0)">
                <FormItem label="角色名称" prop="name">
                    <Input v-model="role.name" placeholder="输入角色名称..."></Input>
                </FormItem>
                <FormItem label="角色状态">
                    <i-switch :true-value="1" :false-value="0" v-model="role.status">
                        <span slot="on">正常</span>
                        <span slot="off">禁用</span>
                    </i-switch>
                </FormItem>
                <FormItem>
                    <Button style="margin-right: 8px" @click="showModalStatus = false">取消</Button>
                    <Button type="primary" @click="createRole('roleValidate')">提交</Button>
                </FormItem>
            </Form>
        </Drawer>
        <Drawer :closable="false" :mask-closable="false" :scrollable="true" title="修改角色" width="640"
            v-model="showEditStatus">
            <Spin size="large" fix v-if="loading"></Spin>
            <Form :label-width="80" :model="role" ref="roleValidate" :rules="roleValidate" action="javascript:void(0)">
                <FormItem label="角色名称" prop="name">
                    <Input v-model="role.name" placeholder="输入角色名称..."></Input>
                </FormItem>
                <FormItem label="角色状态">
                    <i-switch :true-value="1" :false-value="0" v-model="role.status">
                        <span slot="on">正常</span>
                        <span slot="off">禁用</span>
                    </i-switch>
                </FormItem>
                <FormItem>
                    <Button style="margin-right: 8px" @click="showEditStatus = false">取消</Button>
                    <Button type="primary" @click="editRole('roleValidate')">提交</Button>
                </FormItem>
            </Form>
        </Drawer>
        <!-- 权限的弹出 -->
        <Drawer :closable="false" :mask-closable="false" :scrollable="true" title="修改权限" width="640"
            v-model="showPrivStatus">
            <Spin size="large" fix v-if="loading"></Spin>
            <Tree :data="privTree" ref="privSelect" show-checkbox multiple></Tree>
            <div class="drawer-footer">
                <Button style="margin-right: 8px" @click="showPrivStatus = false">取消</Button>
                <Button type="primary" @click="updatePriv">提交</Button>
            </div>
        </Drawer>
        <!-- 用户弹出 -->
        <Drawer :closable="false" :scrollable="true" title="修改权限" width="640" v-model="showAdminStatus">
            <Spin size="large" fix v-if="loading"></Spin>
            <Table border :columns="adminTableList" ref="adminList" :data="adminlist" :loading="dataloading">
                <template #action="{ row,index }">
                    <Button type="error" size="small" @click="removeAdmin(index, row.id)">删除</Button>
                </template>
            </Table>
            <div class="drawer-footer">
                <Button style="margin-right: 8px" @click="showAdminStatus = false">关闭</Button>
            </div>
        </Drawer>
    </div>
</template>

<script>
export default {
    name: 'RoleList',
    data() {
        return {
            loading: false,
            dataloading: true,
            detail_id: 0,
            formItem: {
                'key': ''
            },
            pages: {
                current: 1,
                total: 0,
                size: 10
            },
            list: [
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                {
                    title: 'Id',
                    key: 'id',
                    width: 60,
                },
                {
                    title: '名称',
                    minWidth: 300,
                    key: 'name'
                },
                {
                    title: '状态',
                    slot: 'status',
                    width: 150,
                },
                {
                    title: '操作',
                    slot: 'action',
                    width: 240,
                    align: 'left',
                }
            ],
            tablelist: [],
            role: {
                name: '',
                status: 1
            },
            showEditStatus: false,
            showModalStatus: false,
            showPrivStatus: false,
            showAdminStatus: false,
            roleValidate: {
                name: [
                    { required: true, message: '角色名称必须填写', trigger: 'blur' }
                ]
            },
            privTree: [],
            selectData: [],
            adminlist: [],
            adminTableList: [
                {
                    title: '用户名',
                    key: 'name'
                },
                {
                    title: '手机号',
                    key: 'phone'
                },
                {
                    title: '操作',
                    slot: 'action',
                    width: 80,
                }
            ],
        }
    },
    created() {
        // 取数据
        this.getTableList();
    },
    methods: {
        changePage() {
            this.pages.current = this.$refs['listPage'].currentPage;
            this.getTableList();
        },
        getTableList() {
            var params = { page: this.pages.current };
            this.$api.role.list(params).then(res => {
                this.tablelist = res.result.list;
                this.pages.total = res.result.count
            }).finally(e => {
                this.dataloading = false;
            });
        },
        // 选择id
        changeData(index) {
            this.selectData = index
        },
        // 展开添加
        showModel() {
            this.showModalStatus = !this.showModalStatus;
            this.detail_id = 0
            this.role = {
                name: '',
                status: 1
            }
        },
        showEditModal(value) {
            this.detail_id = value;
            this.showEditStatus = !this.showEditStatus;
            // 取树形菜单
            this.$api.role.detail({ detail_id: value }).then(res => {
                this.role.name = res.result.name;
                this.role.status = res.result.status;
            });
        },
        showPrivModal(value) {
            this.detail_id = value;
            this.showPrivStatus = !this.showPrivStatus;
            // 取树形菜单
            this.$api.role.priv({ detail_id: value }).then(res => {
                this.privTree = res.result;
            });
        },
        // 添加
        createRole(name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    this.loading = true
                    this.$api.role.create({ name: this.role.name, status: this.role.status }).then(res => {
                        this.showModalStatus = !this.showModalStatus;
                        this.role.name = '';
                        this.getTableList();
                    }).finally(e=>{

                    });
                    this.loading = false
                } else {
                    this.$Message.error('请检查输入的信息是否正确！');
                    this.loading = false
                }
            })
        },
        // 修改名称
        editRole(name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    this.loading = true;
                    this.$api.role.edit({ detail_id: this.detail_id, name: this.role.name, status: this.role.status }).then(res => {
                        this.showEditStatus = !this.showEditStatus;
                        this.getTableList();
                    }).finally(res => {
                        this.loading = false;
                    });
                } else {
                    this.$Message.error('请检查输入的信息是否正确！');
                    this.loading = false
                }
            })
        },
        // 删除
        remove(index, id) {
            // 弹出确认框
            this.$Modal.confirm({
                title: '警告',
                content: '<p>此操作不可恢复，三思而后行...</p>',
                onOk: () => {
                    this.$api.role.remove({ detail_id: [id] }).then(res => {
                        this.tablelist.splice(index, 1);
                    });
                }
            });
        },
        // 修改状态
        changeStatus(index, value) {
            this.$api.role.status({ detail_id: index, status: value }).then(res => {});
        },
        // 筛选
        renderTable(name) {
            this.dataloading = true;
            this.pages.currentPage = 1;
            var ps = { 'key': this.formItem.key, 'page': 1 };
            this.$api.role.list(ps).then(res => {
                this.tablelist = res.result.list;
                this.pages.total = res.result.count
            }).finally(e=>{
                this.dataloading = false;
            });
            return;
        },
        updatePriv() {
            this.loading = true
            var node = this.$refs['privSelect'].getCheckedAndIndeterminateNodes()
            var menu_id = [];
            node.forEach((item, index) => {
                menu_id.push(item.menu_id)
            });
            // 提交
            this.$api.role.updatepriv({ detail_id: this.detail_id, menu_id: menu_id }).then(res => {
                this.showPrivStatus = !this.showPrivStatus;
            }).finally(e=>{
                this.loading = false
            });
        },
        // 批量删除
        deleteData() {
            // 弹出确认框
            this.$Modal.confirm({
                title: '警告',
                content: '<p>此操作不可恢复，三思而后行...</p>',
                onOk: () => {
                    var ids = [];
                    this.selectData.forEach((item, index) => {
                        ids.push(item.id);
                    })
                    this.$api.role.remove({ detail_id: ids }).then(res => {
                        this.getTableList();
                    });
                }
            });
        },
        showAdmin(id) {
            this.showAdminStatus = !this.showAdminStatus;
            this.detail_id = id;
            this.$api.role.adminlist({ detail_id: id }).then(res => {
                this.adminlist = res.result;
            }).finally(e=>{
                this.dataloading = false;
                this.loading = false;
            });
            return;
        },
        // 删除
        removeAdmin(index, id) {
            // 弹出确认框
            this.$Modal.confirm({
                title: '警告',
                content: '<p>此操作不可恢复，三思而后行...</p>',
                onOk: () => {
                    this.$api.role.removeadmin({ admin_id: id, detail_id: this.detail_id }).then(res => {
                        this.adminlist.splice(index, 1);
                    });
                }
            });
        },
    }
}
</script>
